/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Chat;

import TemplateParser.TemplateParsingServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 *
 * @author Tinoo
 */
public class ProfileTServlet extends TemplateParsingServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            Enumeration attrEnum = request.getAttributeNames();
            Enumeration paramEnum = request.getParameterNames();

            //handle the Modify button being pressed:
            String strModify = request.getParameter("ModifyBtn");
            if (strModify!=null &&strModify.equals("Modify"))
            {
                HttpSession session = request.getSession();
                if (session != null)
                {
                    Integer userId = (Integer)session.getAttribute(ChatBusinessLogic.AttrUserId);
                    String strNewPass = request.getParameter("password");
                    String strNewDescr = request.getParameter("details");

                    if (userId != null && strNewPass != null && strNewDescr != null)
                    {
                        ChatBusinessLogic.Instance().UpdateUserInfo(userId, strNewPass, strNewDescr);
                    }
                }
            }

            String strRet = parseTemplateFile("HTML Templates/ModifyAccount/templModifyAccount.html", request);
            out.print(strRet);
        } finally {
            out.close();
        }
    }

    public String showModifyAccountForm(Object request)
    {
        String strRet;
        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();
        Menu menu = new Menu();
        strRet = menu.printMenu(req);
        boolean bShowUpdateInfoForm = (session != null &&
                                       session.getAttribute(ChatBusinessLogic.AttrUserId) != null);

         if (bShowUpdateInfoForm)
             strRet += parseTemplateFile("HTML Templates/ModifyAccount/templModifyAccountForm.html", req);

         return strRet;
    }

    public String fillDetails(Object request)
    {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();
        if (session == null)
            return "";
        Integer userID = (Integer)session.getAttribute(ChatBusinessLogic.AttrUserId);
        if (userID == null)
            return "";
        return ChatBusinessLogic.Instance().GetUserInfo(userID);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Update Profile Servlet";
    }// </editor-fold>

}
